#include <vector>
#include <iostream>
#include <algorithm>

using namespace std;

class Solution 
{
    static bool myCamp(const vector<int> & a, const vector<int> & b)
    {
        return a[0] < b[0];
    }
public:
    vector<vector<int>> merge(vector<vector<int>>& intervals) 
    {
        vector<vector<int>> result;
        if(intervals.empty())
        {
            return result;
        }
        sort(intervals.begin(), intervals.end(), myCamp);
        result.push_back(intervals[0]);
        for(int i = 1; i < intervals.size(); i++)
        {
            vector<int> last = result.back();
            if(intervals[i][0] <= last[1])
            {
                if(intervals[i][1] > last[1])
                {
                    last[1] = intervals[i][1];
                    result.pop_back();
                    result.push_back(last);
                }
            }
            else
            {
                result.push_back(intervals[i]);
            }
        }
        return result;
    }
};

// 辅助函数：打印区间数组
void printIntervals(const vector<vector<int>>& intervals) 
{
    cout << "[";
    for(int i = 0; i < intervals.size(); i++) 
    {
        cout << "[" << intervals[i][0] << "," << intervals[i][1] << "]";
        if(i < intervals.size() - 1) cout << ",";
    }
    cout << "]";
}

int main() 
{
    Solution solution;
    
    // 测试案例1: [[1,3],[2,6],[8,10],[15,18]]
    cout << "测试案例1:" << endl;
    vector<vector<int>> intervals1 = {{1, 3}, {2, 6}, {8, 10}, {15, 18}};
    cout << "输入: intervals = ";
    printIntervals(intervals1);
    cout << endl;
    vector<vector<int>> result1 = solution.merge(intervals1);
    cout << "输出: ";
    printIntervals(result1);
    cout << endl;
    cout << "期望: [[1,6],[8,10],[15,18]]" << endl << endl;
    
    // 测试案例2: [[1,4],[4,5]]
    cout << "测试案例2:" << endl;
    vector<vector<int>> intervals2 = {{1, 4}, {4, 5}};
    cout << "输入: intervals = ";
    printIntervals(intervals2);
    cout << endl;
    vector<vector<int>> result2 = solution.merge(intervals2);
    cout << "输出: ";
    printIntervals(result2);
    cout << endl;
    cout << "期望: [[1,5]]" << endl << endl;
    
    // 测试案例3: [[1,4],[2,3]]
    cout << "测试案例3:" << endl;
    vector<vector<int>> intervals3 = {{1, 4}, {2, 3}};
    cout << "输入: intervals = ";
    printIntervals(intervals3);
    cout << endl;
    vector<vector<int>> result3 = solution.merge(intervals3);
    cout << "输出: ";
    printIntervals(result3);
    cout << endl;
    cout << "期望: [[1,4]]" << endl << endl;
    
    // 测试案例4: 单个区间
    cout << "测试案例4 (单个区间):" << endl;
    vector<vector<int>> intervals4 = {{1, 4}};
    cout << "输入: intervals = ";
    printIntervals(intervals4);
    cout << endl;
    vector<vector<int>> result4 = solution.merge(intervals4);
    cout << "输出: ";
    printIntervals(result4);
    cout << endl;
    cout << "期望: [[1,4]]" << endl << endl;
    
    // 测试案例5: 无重叠
    cout << "测试案例5 (无重叠):" << endl;
    vector<vector<int>> intervals5 = {{1, 2}, {3, 4}, {5, 6}};
    cout << "输入: intervals = ";
    printIntervals(intervals5);
    cout << endl;
    vector<vector<int>> result5 = solution.merge(intervals5);
    cout << "输出: ";
    printIntervals(result5);
    cout << endl;
    cout << "期望: [[1,2],[3,4],[5,6]]" << endl << endl;
    
    // 测试案例6: 全部重叠
    cout << "测试案例6 (全部重叠):" << endl;
    vector<vector<int>> intervals6 = {{1, 4}, {2, 5}, {3, 6}};
    cout << "输入: intervals = ";
    printIntervals(intervals6);
    cout << endl;
    vector<vector<int>> result6 = solution.merge(intervals6);
    cout << "输出: ";
    printIntervals(result6);
    cout << endl;
    cout << "期望: [[1,6]]" << endl << endl;
    
    // 测试案例7: 乱序输入
    cout << "测试案例7 (乱序输入):" << endl;
    vector<vector<int>> intervals7 = {{8, 10}, {1, 3}, {15, 18}, {2, 6}};
    cout << "输入: intervals = ";
    printIntervals(intervals7);
    cout << endl;
    vector<vector<int>> result7 = solution.merge(intervals7);
    cout << "输出: ";
    printIntervals(result7);
    cout << endl;
    cout << "期望: [[1,6],[8,10],[15,18]]" << endl << endl;
    
    // 测试案例8: 包含关系
    cout << "测试案例8 (包含关系):" << endl;
    vector<vector<int>> intervals8 = {{1, 10}, {2, 3}, {4, 5}, {6, 7}, {8, 9}};
    cout << "输入: intervals = ";
    printIntervals(intervals8);
    cout << endl;
    vector<vector<int>> result8 = solution.merge(intervals8);
    cout << "输出: ";
    printIntervals(result8);
    cout << endl;
    cout << "期望: [[1,10]]" << endl << endl;
    
    // 测试案例9: 相邻区间
    cout << "测试案例9 (相邻区间):" << endl;
    vector<vector<int>> intervals9 = {{1, 3}, {3, 5}, {5, 7}};
    cout << "输入: intervals = ";
    printIntervals(intervals9);
    cout << endl;
    vector<vector<int>> result9 = solution.merge(intervals9);
    cout << "输出: ";
    printIntervals(result9);
    cout << endl;
    cout << "期望: [[1,7]]" << endl << endl;
    
    // 测试案例10: 空数组
    cout << "测试案例10 (空数组):" << endl;
    vector<vector<int>> intervals10 = {};
    cout << "输入: intervals = ";
    printIntervals(intervals10);
    cout << endl;
    vector<vector<int>> result10 = solution.merge(intervals10);
    cout << "输出: ";
    printIntervals(result10);
    cout << endl;
    cout << "期望: []" << endl << endl;
    
    return 0;
}